package com.lb.myeam.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lb.myeam.common.utils.DBUtil;
import com.lb.myeam.core.dao.MenuDao;
import com.lb.myeam.core.dto.TreeDTO;
import com.lb.myeam.core.entity.Menu;
import com.mysql.jdbc.Statement;

public class MenuDaoImpl implements MenuDao {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;

	public boolean save(Menu menu) throws Exception {
		conn = DBUtil.getConn();
		String sql = "insert into menu values (?,?,?,?,?,?)";
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, menu.getId());
		pstmt.setString(2, menu.getName());
		pstmt.setString(3, menu.getUrl());
		pstmt.setInt(4, menu.getChecked());
		pstmt.setString(5, menu.getIcon());
		pstmt.setInt(6, menu.getParent_id());
		int result = pstmt.executeUpdate();
		return result > 0;
	}

	public boolean update(Menu menu) throws Exception {
		conn = DBUtil.getConn();
		String sql = "update menu set name=\'"+
				menu.getName()+"\',url=\'"+
				menu.getUrl()+"\' where id=\'"+
				menu.getId()+"\'";
		
		pstmt=conn.prepareStatement(sql);
		int result=pstmt.executeUpdate();
		return result>0;
	}

	public boolean delete(int id) throws Exception {
		conn = DBUtil.getConn();
		String sql = "delete menu where id=?";
		conn.prepareStatement(sql);
		pstmt.setInt(1, id);
		int result = pstmt.executeUpdate();
		return result > 0;
	}

	public List<Menu> findAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Menu findById(int id) throws Exception {
		conn = DBUtil.getConn();
		String sql = "select * from menu where id=?";
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, id);

		rs = pstmt.executeQuery();
		Menu m = new Menu();
		while (rs.next()) {
			m.setId(rs.getInt(1));
			m.setName(rs.getString(2));
			m.setUrl(rs.getString(3));
			m.setChecked(rs.getInt(4));
			m.setIcon(rs.getString(5));
			m.setParent_id(rs.getInt(6));
		}
		return m;
	}

	/**
	 * 根据父id获取子节点们
	 */
	public List<TreeDTO> getChildrenByParentId(String id) throws Exception {
		conn = DBUtil.getConn();
		String sql = "";
		if ("".equals(id) || id == null) {
			sql = "select * from menu where parent_id = 999999";
		} else {
			sql = "select * from menu where parent_id = " + id;
		}
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();

		List<Menu> mlist = new ArrayList<Menu>();
		while (rs.next()) {
			Menu m = new Menu();
			m.setId(rs.getInt("id"));
			m.setIcon(rs.getString("icon"));
			m.setChecked(rs.getInt("checked"));
			m.setName(rs.getString("name"));
			m.setUrl(rs.getString("url"));
			m.setParent_id(rs.getInt("parent_id"));
			mlist.add(m);
		}

		List<TreeDTO> tlist = new ArrayList<TreeDTO>();
		for (Iterator iterator = mlist.iterator(); iterator.hasNext();) {
			Menu menu = (Menu) iterator.next();
			TreeDTO tree = new TreeDTO();
			tree.setId(menu.getId());
			tree.setText(menu.getName());
			tree.setChecked(menu.getChecked());
			tree.setIconCls(menu.getIcon());
			tree.setParent_id(menu.getParent_id());
			if (getChildren(menu.getId()).size() > 0) {
				tree.setState("closed");
			} else {
				tree.setState("open");
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("url", menu.getUrl());
			tree.setAttributes(map);
			tlist.add(tree);
		}

		return tlist;
	}

	/**
	 * 根据pid 获取孩子
	 * 
	 * @param pid
	 * @return
	 * @throws Exception
	 */
	public List<Menu> getChildren(int pid) throws Exception {
		conn = DBUtil.getConn();
		String sql = "select * from menu where parent_id = " + pid;
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();

		List<Menu> children = new ArrayList<Menu>();
		while (rs.next()) {
			Menu r = new Menu();
			r.setId(rs.getInt("id"));
			r.setIcon(rs.getString("icon"));
			r.setChecked(rs.getInt("checked"));
			r.setName(rs.getString("name"));
			r.setUrl(rs.getString("url"));
			r.setParent_id(rs.getInt("parent_id"));
			children.add(r);
		}

		return children;
	}

}
